/* global window */
import pathToRegexp from 'path-to-regexp'
import modelExtend from 'dva-model-extend'
import { config } from 'utils'
import { create, remove, update, query, uploadfile, getImg, getNumber } from 'services/customer.js'
import { message } from 'antd'
import * as basicData from 'services/basicData.js'
import { pageModel } from '../common'
import { uploadPhoto } from '../../services/customer'

export default modelExtend(pageModel, {
  namespace: 'customerDetail',

  state: {
    editType: 'create',
    data: {},
    countryCodeData: [],
    licenseType1: '1',
    licenseType2: '1',
    fileList: [],
    identifyTypeList: [],
    identifierTypeData: [],
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/customer/detail/:ID').exec(pathname.pathname)
        if (match) {
          if (match[1] !== config.ADDFLAG) {
            dispatch({ type: 'query', payload: { id: match[1] } })
          } else {
            dispatch({ type: 'getNumber' })
            dispatch({
              type: 'updateState',
              payload: {
                editType: 'create',
                data: {},
                countryCodeData: [],
                licenseType1: '1',
                licenseType2: '1',
              },
            })
          }
          dispatch({ type: 'iniBasicData' })
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(query, payload)
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            editType: 'update',
            data: data.data,
            identifyTypeList: data.data.identifications,
          },
        })
      } else {
        throw data
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.customerDetail)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },
    * uploadfile ({ payload }, { call, put, select }) {
      const data = yield call(uploadfile, payload)
      if (data.success) {
        message.success('upload success')
        yield put({
          type: 'getImg',
          payload: {
            fileMd5: data.data,
          },
        })
      } else {
        throw data
      }
    },
    * getImg ({ payload }, { call, put, select }) {
      const { fileList } = yield select(_ => _.customerDetail)
      const img = yield call(getImg, { fileMd5: payload.fileMd5.fileMd5 })
      fileList.push({ ...payload.fileMd5, url: img, uid: fileList.length + 1 })
      yield put({
        type: 'updateState',
        payload: {
          fileList,
        },
      })
    },

    * getNumber ({ payload }, { call, put, select }) {
      const customerNo = yield call(getNumber, {})
      yield put({
        type: 'updateState',
        payload: {
          data: {
            customerNumber: customerNo.data,
          },
        },
      })
    },
    * create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'query', payload: { id: data.data } })
        yield put({
          type: 'updateState',
          payload: {
            editType: 'update',
          },
        })
      } else {
        throw data
      }
    },

    * update ({ payload }, { call, put }) {
      const data = yield call(update, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'query', payload: { id: data.data } })
      } else {
        throw data
      }
    },

    * iniBasicData ({ payload = {} }, { call, put }) {
      const countryCodeData = yield call(basicData.listall, { type: 'CountryCodes' })
      if (countryCodeData.success) {
        yield put({
          type: 'updateState',
          payload: {
            countryCodeData: countryCodeData.data,
          },
        })
      }
      const identifierType = yield call(basicData.listall, { type: 'IdentifierType' })
      if (identifierType.success) {
        yield put({
          type: 'updateState',
          payload: {
            identifierTypeData: identifierType.data,
          },
        })
      }
    },
    // * getIdentifyTypeList ({ payload = {} }, { call, put }) {
    //   const identifyTypeData = yield call(identification.list, payload)
    //   if (identifyTypeData) {
    //     yield put({
    //       type: 'updateState',
    //       payload: {
    //         identifyTypeList: identifyTypeData.data,
    //       },
    //     })
    //   }
    // },
  },
  reducers: {
    updatePerson (state, { payload }) {
      state.data.person = { ...state.data.person, ...payload }
      return {
        ...state,
      }
    },
    updateCustomer (state, { payload }) {
      state.data = { ...state.data, ...payload }
      return {
        ...state,
      }
    },
  },
})
